package datapool.define
{
	import flash.geom.Point;
	/**
	 * @author Administrator
	 */
	public class DirectionDefine
	{
		//下
        public static var DIR_X:String   = "x";
        
        //左下
        public static var DIR_Z_X:String = "zx";
        
        // 左
        public static var DIR_Z:String   = "z";
        
        //左上
        public static var DIR_Z_S:String = "zs";
        
        //上
        public static var DIR_S:String   = "s";
        
        //右上
        public static var DIR_Y_S:String = "ys";
        
        //右
        public static var DIR_Y:String   = "y";
        
        //右下
        public static var DIR_Y_X:String = "yx";
        
        //非法方向
        public static var DIR_ERR:String = "dir_err";
        
        static public function caculateDirection(srcPos:Point, dstPos:Point):String
        {
        	var dir:String;
       		var disY:int = Math.abs(dstPos.y - srcPos.y);
       		var disX:int = Math.abs(dstPos.x - srcPos.x);
       		
       		if(!srcPos || !dstPos)
        	{
        		return DIR_ERR;
        	}
       		
       		if(!disY && !disX)
       		{
       			return DIR_ERR;
       		}
       		
			if(!disY)
			{
				if (dstPos.x - srcPos.x > 0)
				{
					dir = DIR_Y;
				}
				dir = DIR_Z;
			}
			else if(!disX)
			{
				if(dstPos.y-srcPos.y>0)
				{
					dir = DIR_X;
				}
				else
				{
					dir = DIR_S;
				}
			}
			else
			{
				var degree:Number = Math.atan(disY/disX);   //与x轴的夹角 y轴向下增长
				if((dstPos.x-srcPos.x>0) && (dstPos.y-srcPos.y>0))
				{
					if(degree<Math.PI/8)
					{
						dir = DIR_Y;
					}
					else if(degree<(Math.PI*3)/8)
					{
						dir = DIR_Y_X;
					}
					else
					{
						dir = DIR_X;
					}
				}
				else if((dstPos.x-srcPos.x>0) && (dstPos.y-srcPos.y<0))
				{
					if(degree<Math.PI/8)
					{
						dir = DIR_Y;
					}
					else if(degree<(Math.PI*3)/8)
					{
						dir = DIR_Y_S;
					}
					else
					{
						dir = DIR_S;
					}
				}
					
				else if((dstPos.x-srcPos.x<0) && (dstPos.y-srcPos.y>0))
				{
					if(degree<Math.PI/8)
					{
						dir = DIR_Z;
					}
					else if(degree<(Math.PI*3)/8)
					{
						dir = DIR_Z_X;
					}
					else
					{
						dir = DIR_X;
					}
				}
					
				else if((dstPos.x-srcPos.x<0) && (dstPos.y-srcPos.y<0))
				{
					if(degree<Math.PI/8)
					{
						dir = DIR_Z;
					}
					else if(degree<(Math.PI*3)/8)
					{
						dir = DIR_Z_S;
					}
					else
					{
						dir = DIR_S;
					}
				}
			}
			
			return dir;
        }
		
		static public function caculateSpeed(srcPos:Point, dstPos:Point, speed:int):Point
		{
			var speedPoint:Point;
			var disX:int;
			var disY:int;
			var distance:Number;
			
			if(!srcPos && !dstPos)
			{
				return null;
			}

			speedPoint = new Point();
			if (srcPos.equals(dstPos))
			{
				speedPoint.x = 0;
				speedPoint.y = 0;
			}
			else if(srcPos.x == dstPos.x)
			{
				speedPoint.x = 0;
				if (dstPos.y > srcPos.y)
				{
					speedPoint.y = speed;
				}
				else
				{
					speedPoint.y = -speed;
				}
			}
			else if(srcPos.y == dstPos.y)
			{
				speedPoint.y = 0;
				if (dstPos.x > srcPos.x)
				{
					speedPoint.x = speed;
				}
				else
				{
					speedPoint.x = -speed;
				}
			}
			else
			{
				disX = dstPos.x - srcPos.x;
				disY = dstPos.y - srcPos.y;
				distance = Point.distance(srcPos, dstPos);
				speedPoint.x = speed*disX/distance;
				speedPoint.y = speed*disY/distance;
			}
			return speedPoint;
		}
	}
}
